
#import "BIDSettingsDetailTableViewController.h"
#import "BIDRepository.h"

@interface BIDSettingsDetailTableViewController ()

@end

@implementation BIDSettingsDetailTableViewController{
    NSString *initialText;
    BOOL hasChanged;
}

- (NSString *)dataFilePath{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [paths objectAtIndex:0];
    return [documentDirectory stringByAppendingPathComponent:@"data.archive"];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.titles = @[@"Name",@"URL",@"Loginname",@"Password"];
    if (self.repository == nil){
        self.repository = [[BIDRepository alloc] init];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    if ([self.title isEqualToString:@"Repository"]){
        return 1;
    }else{
        return 0;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    if ([self.title isEqualToString:@"Repository"]){
        return 4;
    }else{
        return 0;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"plainCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if ([self.title isEqualToString:@"Repository"]){
        // Configure the cell...
        UILabel *label = (id)[cell viewWithTag:2048];
        label.text = self.titles[indexPath.row];
        UITextField *textField = (id)[cell viewWithTag:4096];
        textField.superview.tag = indexPath.row;//use this to temporary store indexPath.row for the next usage in textFieldDidEndEditing
        if ([label.text isEqualToString:@"Password"]){
            textField.secureTextEntry = YES;
        }
        switch (indexPath.row) {
            case 0:
                textField.text = self.repository.name;
                break;
            case 1:
                textField.text = self.repository.url;
                break;
            case 2:
                textField.text = self.repository.loginname;
                break;
            case 3:
                textField.text = self.repository.password;
                break;
            default:
                break;
        }
    }
    return cell;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    initialText = textField.text;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    //TODO: need to verify the input data: format and size
    if (![textField.text isEqualToString:initialText]){
        hasChanged = YES;
        switch (textField.superview.tag) {
            case 0://name
                self.repository.name = textField.text;
                break;
            case 1://url
                self.repository.url = textField.text;
                break;
            case 2://username
                self.repository.loginname = textField.text;
                break;
            case 3://password
                self.repository.password = textField.text;
                break;
            default:
                break;
        }
    }
    //NSLog(@"=============================================");
    //NSLog(@"superview.tag %d", textField.superview.tag);
    //NSLog(@"textField %@", textField.text);
    //NSLog(@"name %@", self.repository.name);
    //NSLog(@"url  %@", self.repository.url);
    //NSLog(@"loginname %@", self.repository.loginname);
    ///NSLog(@"password %@", self.repository.password);
    //NSLog(@"=============================================");
}
#pragma mark - Table view delegate

- (void)cancel:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)save:(id)sender{
    [self.view endEditing:YES];
    if (hasChanged == YES){
        //NSLog(@"self.repository.name %@", self.repository.name);
        //NSLog(@"=============================================");
        NSString *filePath = [self dataFilePath];
        NSMutableData *data = [[NSMutableData alloc] init];
        NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
        [archiver encodeObject:self.repository forKey:@"repository"];
        [archiver finishEncoding];
        [data writeToFile:filePath atomically:YES];
        [self.delegate settingsDetailTableViewController:self addRepository:self.repository];
    }
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)textFieldDone:(id)sender{
    [sender resignFirstResponder];
}
@end
